package com.zhong.gang.app.ui.carorder;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.api.HttpUtils;
import com.zhong.gang.app.ui.address.AddressAct;
import com.zhong.gang.app.ui.address.AddressBean;
import com.zhong.gang.app.utils.AddressListener;
import com.zhong.gang.applib.R;
import com.zhong.gang.applib.base.act.ActBaseRefresh;
import com.zhong.gang.applib.databinding.LayoutFindBinding;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.modle.ListBean;
import com.zhong.gang.applib.ui.view.RightTitleView;
import java.util.ArrayList;
import java.util.List;

public class CarOrderAct extends ActBaseRefresh<LayoutFindBinding, OrderNumAdapter>
        implements BaseQuickAdapter.OnItemClickListener  , AddressListener {
    private static OrderListener listener;
    private RightTitleView rightTitleView;
    private String steelMillId = "";
    private List<OrderNumBean> dataList = new ArrayList<>();
    public static final void openAct(Context context, OrderListener listener){
        Intent intent = new Intent(context,CarOrderAct.class);
        context.startActivity(intent);
        CarOrderAct.listener = listener;
    }


    @Override
    public int LayoutRes() {
        return R.layout.layout_find;
    }

    @NonNull
    @Override
    public String CreateTitle() {
        return "选择内容";
    }

    @Override
    public void initView() {
        bind.selectLayout.setVisibility(View.GONE);
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initrefrsh(bind.pullto,new OrderNumAdapter(),bind.recyclerView);
        adapter.setOnItemClickListener(this);
        getTitleBar().setRightView(rightTitleView = new RightTitleView(this));
        rightTitleView.setContent("选择钢厂");
        rightTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddressAct.openAct(CarOrderAct.this,Api.gangChangeList,CarOrderAct.this);
            }
        });
        bind.phone.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                /*判断是否是“搜索”键*/
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                    onRefresh();
                    return true;
                }
                return false;
            }
        });
        bind.find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });
        getData();
    }

    @Override
    public void getData() {
        super.getData();
        HttpUtils.get(Api.sendCarList,this)
                .addParams("current",page+"")
                .addParams("endLocal",steelMillId)
                .addParams("size","10")
                .build().execute(new ActCallBack<BaseDataBean<ListBean<OrderNumBean>>>(){
            @Override
            public void onResponse(BaseDataBean<ListBean<OrderNumBean>> data) throws Exception {
                super.onResponse(data);
                if (page == 1)
                    dataList = data.getResult().getRecords();
                else dataList.addAll(data.getResult().getRecords());
                setData(data.getResult().getRecords());
            }

            @Override
            public void onError(String str) {
                super.onError(str);
                setData(new ArrayList<OrderNumBean>());
            }
        });

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        OrderNumBean bean = (OrderNumBean) adapter.getItem(position);
        closeActivity();
        if (listener != null)
            listener.onOrderListener(bean);
    }
    @Override
    protected void onPause() {
        super.onPause();
        hideKeyboard();
    }

    public  void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // 隐藏软键盘
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
    }

    @Override
    public void onAddressBack(AddressBean addressBean) {
        steelMillId = addressBean.getId()+"";
        onRefresh();
    }
}
